
package edu.chl.angholt.bookproject.db;




import javax.persistence.EntityManagerFactory;
import javax.persistence.Persistence;

/**
 * A locator for JPA controllers and others...
 * 
 * @author Jonas
 */
public final class Database {

    private static final String pu =  "bookproject_pu";
    private static final String puTest =  "bookproject_pu_test";
    private static boolean test = false;
    //EMF for testing
    private static EntityManagerFactory emf = null;
    
    //EMF for real use
//    private static final EntityManagerFactory emf = Persistence.createEntityManagerFactory("bookproject_pu");
    
    private static AuthorJpaCtrl authorCtrl; 
    private static BookJpaCtrl bookCtrl; 
    private static UserJpaCtrl userCtrl;
    
    private Database() {
    }
    
    public static AuthorJpaCtrl getAuthorController() {
        make();
        return authorCtrl;
    }
    
    public static BookJpaCtrl getBookController() {
        make();
        return bookCtrl;
    }
    
    public static UserJpaCtrl getUserController() {
        make();
        return userCtrl;
    }
    
    public static void startAsTest(){
        test = true;
    }
    
    private static void make(){
        if(emf==null){
            String name;
            if(test)
                name=puTest;
            else
                name=pu;
            emf = Persistence.createEntityManagerFactory(name);        
            authorCtrl = new AuthorJpaCtrl(emf); 
            bookCtrl = new BookJpaCtrl(emf); 
            userCtrl = new UserJpaCtrl(emf);
        }
    }
    
}
